﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using park.cn.linkit.epark;

namespace park.Equipment
{
    public partial class CollectorList : System.Web.UI.Page
    {
        ServiceList ws = new ServiceList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["m"] != null)
            {
                Response.ContentType = "application/json";
                string m = Request.Params["m"].ToLower();
                string r = "{}";
                switch (m)
                {
                    case "search":
                        r = Search();
                        break;
                    case "listSetRelation":
                        r = listSetRelation();
                        break;
                    case "delete":
                        r = DeleteCollector();
                        break;
                    case"getinfo":
                        r = GetInfo();
                        break;
                    case "edit":
                        r = Edit();
                        break;
                    case "add":
                        r = Add();
                        break;

                }
                Response.Write(r);
                Response.End();
            }
            
        }

        string fix(string key)
        {
            if (Request.Params[key] != null)
            {
                return Request.Params[key];
            }
            return "";
        }

        string Search()
        {
            string cid = fix("cid");
            string aid = fix("aid");
            string sid = fix("sid");
            string code = "";
            try
            {
                code = Uri.UnescapeDataString(fix("code"));
            }
            catch
            {
            }
            string pageIndex = fix("pageIndex");
            string pageSize = fix("pageSize");
            
            string result = @"{""ListCollector"":[
                        {
                              ""first"":
                              {
                                ""ID"":""001"",
                                ""Relationship"":""主收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                              },
                              ""second"":
                              {
                                ""ID"":""002"",
                                ""Relationship"":""辅收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                            }
                        },{
                              ""first"":
                              {
                                ""ID"":""0011"",
                                ""Relationship"":""主收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                              },
                              ""second"":
                              {
                                ""ID"":""0021"",
                                ""Relationship"":""辅收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                            }
                        },{
                              ""first"":
                              {
                                ""ID"":""00111"",
                                ""Relationship"":""主收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                              },
                              ""second"":
                              {
                                ""ID"":""00211"",
                                ""Relationship"":""辅收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                            }
                        },{
                              ""first"":
                              {
                                ""ID"":""10011"",
                                ""Relationship"":""主收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                              },
                              ""second"":
                              {
                                ""ID"":""10021"",
                                ""Relationship"":""辅收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                            }
                        },{
                              ""first"":
                              {
                                ""ID"":""110011"",
                                ""Relationship"":""主收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                              },
                              ""second"":
                              {
                                ""ID"":""110021"",
                                ""Relationship"":""辅收集器"",
                                ""Code"":""TSN4564"",
                                ""Longitude"":""86.25"",
                                ""Latitude"":""73.19"",
                                ""Connection"":""18/627870756"",
                                ""Enable"":""启用""
                            }
                        }
                            ],
                            ""RowsCount"":2
                        }";

            result = ws.ListCollector(cid, aid, sid, code, int.Parse(pageIndex), int.Parse(pageSize));
            return result;

        }

        string listSetRelation()
        {
            string id1 = Request.Params["id1"];
            string v1 = Request.Params["v1"];
            string id2 = Request.Params["id2"];
            string v2 = Request.Params["v2"];
            bool r1 = ws.SetCollectorRelationship(id1, v1);
            bool r2 = ws.SetCollectorRelationship(id2, v2);
            if (r1 && r2)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string DeleteCollector()
        {
            string id1 = Request.Params["id1"];
            string id2 = Request.Params["id2"];
            bool r = ws.DeleteCollector(id1, id2);
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string GetInfo()
        {
            string id = Request.Params["id"];
            
            double lng = 114f + new Random(DateTime.Now.Millisecond).NextDouble();
            double lat = 32f + new Random(DateTime.Now.Millisecond).NextDouble();
            string ret = @"{
                    ""CollectorInfo"":
                        {
                            ""id"": """+id+@""",
                            ""Relationship"":""主收集器"",
                            ""Code"":""TSN4564"",
                            ""Longitude"":"+lng+@",
                            ""Latitude"":"+lat+@",
                            ""Connection"":""18/627870756"",
                            ""Status"":""启用""
                        }
                }
                ";

            ret = ws.GetCollectorById(id);
            return ret;
        }

        string Edit()
        {
            string input = Request.Params["info"];
            input = Uri.UnescapeDataString(input);
            bool r = ws.UpdateCollector(input);
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }

        string Add()
        {
            string input = Request.Params["info"];
            input = Uri.UnescapeDataString(input);
            bool r = ws.AddCollector(input);
            if (r)
            {
                return "{\"result\":1}";
            }
            return "{\"result\":0}";
        }
    }
}